from requests import post as POST
from settings import ALIST_HOST,ALIST_PORT,PASSWORD,USERNAME
from urllib.parse import unquote

start_url = 'http://' + ALIST_HOST + ':' + ALIST_PORT   #http://localhost:5244

# print(start_url)



def fs_list(url,token:str)->dict:
    '''获取文件列表信息'''
    fs_tree = POST(url= start_url + '/api/fs/list', 
        data={
            "path": "/" + unquote(url.split(r'/', maxsplit=3)[3]),
            "password":"",
            "page":'1',
            "per_page":'0',
            "refresh":'false'}, 
        headers= {"Authorization": token}
        ).json()
    return fs_tree

def login() ->str:
    '''登录alist，获取token

    ---------------成功结果：
    {
    "code": 200,
    "message": "success",
    "data": {
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNjczNjE2ODAyLCJuYmYiOjE2NzM0NDQwMDIsImlhdCI6MTY3MzQ0NDAwMn0.Ox6nT6aAWrsHwsKmGb_iI3HDGSrtJSb1WoYT1GEzzVg"
    }
    }
    '''
    # 这里必须是json=dict()
    data = POST(url= start_url + '/api/auth/login',
        json={
            "otp_code": "",
            "password": PASSWORD,
            "username": USERNAME}
        ).json()
    return data['data']['token']




if __name__ == '__main__':
    # print(login())
    s = fs_list("http://localhost:5244/all/%E7%AC%94%E8%AE%B0%E5%A4%87%E4%BB%BD/%E9%9F%B3", 
        token=login())
    print(s)


    '''api列表，来自@Kuingsmile  https://github.com/alist-org/alist/discussions/2501
        POST token获取 /api/auth/login
        POST 获取文件列表 /api/fs/list
        POST 新建文件夹 /api/fs/mkdir
        POST 重命名文件/目录 /api/fs/rename
        POST 删除文件/目录 /api/fs/remove
        PUT 表单上传文件 /api/fs/form
        POST 获取下载信息 /api/fs/get
        GET 系统设置 /api/admin/setting/list
        GET 账户列表 /api/admin/user/list
        GET 存储列表 /api/admin/storage/list
        POST 启用存储 /api/admin/storage/enable
        POST 禁用存储 /api/admin/storage/disable
        POST 新建存储 /api/admin/storage/create
        GET 查询指定存储信息 /api/admin/storage/get
        GET 查询所有驱动配置模板列表 /api/admin/driver/list
        POST 删除指定存储 /api/admin/storage/delete
    '''